<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2019/3/9
 * Time: 16:55
 */

namespace app\admin\controller;

use app\admin\model\MoneyLog as MoneyLogModel;
use app\admin\validate\MoneyLog as MoneyLogValidate;
use think\db\Where;

class Orderlist extends Base {

    public function __construct(){
        parent::__construct();
        $this->model = new MoneyLogModel;
        $this->validate = new MoneyLogValidate;
    }

    /**
     * @param int $limit
     * @param int $page
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index($limit=10,$page=1){
        $where=new Where();
        $data = request()->post();
        if(self::$mc_id){
            $where[$this->model->getTable().'.mc_id'] = self::$mc_id;
        }
        if($data){
            if($data['title']){
                $where[$this->model->getTable().'.name']=['like','%'.trim($data['title']).'%'];
            }
            if($data['status']){
                $where[$this->model->getTable().'.type']=$data['status'];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where[$this->model->getTable().'.create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $this->model->getList($where,$limit);
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    public function add($id=0){
        if($id){
            $info = $this->model->find($id);
            $this->assign('info',$info);
        }
        return view();
    }


    /**
     * 文章操作
     */
    public function insert(){
        $data = request()->param();
        if(!$this->validate->check($data)){
            $this->__e($this->validate->getError());
        }
        if(!$data['id']){
            unset($data['id']);
            $data['create_time']=time();
            if(!$this->model->insert($data)){
                $this->__e('添加失败');
            }
            $this->__s('添加成功',['redirect'=>Url('article/index')]);
        }else{
            $data['update_time']=time();
            if(!$this->model->isUpdate(true)->save($data)){
                $this->__e('修改失败');
            }
            $this->__s('修改成功',['redirect'=>Url('mch/index')]);
        }
    }

    public function status($id=0,$status=0){
        if(!$id || !$status){
            $this->__e('缺少参数');
        }
        $mch = $this->model->get($id);
        if(!$mch){
            $this->__e('商户不存在');
        }
        $mch->status = $status;
        if(!$mch->isUpdate(true)->save()){
            $this->__e('操作失败');
        }
        $this->__s('操作成功');
    }


    /**
     * 删除
     * @param int $id
     * @throws \Exception
     */
    public function del($id=0){
        if(!$id){
            $this->__e('缺少参数');
        }
        if(!$this->model->where('id','eq',$id)->delete()){
            $this->__e('删除失败');
        }
        $this->__s('删除成功');
    }
}